package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.mapper.EduCommentMapper;
import com.atguigu.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-04-27
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {

    /**
     * 分页查询评论
     * 最新时间
     * @param current
     * @param limit
     * @param courseId
     * @return
     */
    @Override
    public Map<String, Object> pageList(long current, long limit, String courseId) {

        Page<EduComment> commentPage = new Page<>(current, limit);
        QueryWrapper<EduComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_modified");
        queryWrapper.eq("course_id",courseId);
        baseMapper.selectPage(commentPage,queryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        //total 总数  records 记录数
        long total = commentPage.getTotal();
        List<EduComment> records = commentPage.getRecords();

         map.put("items", records);
         map.put("current",commentPage.getCurrent());
         map.put("pages", commentPage.getPages());
         map.put("size", commentPage.getSize());
         map.put("total", commentPage.getTotal());
         map.put("hasNext", commentPage.hasNext());
        map.put("hasPrevious", commentPage.hasPrevious());
        return map;
    }
}
